package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.config.Equip;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.game.utils.NotifyModules;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.LinkedHashMap;
import com.kitty.game.equip.handler.UpgradeHandler;

import static com.kitty.game.utils.Const.*;

@Component("RecastJewelryHandler")
@Slf4j
public class RecastJewelryHandler extends UpgradeHandler {
    private static final String ERROR_RESOURCES = "请放入正确的装备或材料。";
    private static final String SUCCESS = "恭喜你，花费了{0}文钱、#R{1}#n点首饰精华，成功重铸了#R{2}#n的属性。";


    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip roleEquip = (RoleEquip) params[0];
        Equip equip = (Equip) params[1];
        int needCash=needCash(roleEquip.queryLevel(),0);
        int jewelryEssence = Integer.MAX_VALUE;
        if (roleEquip.queryLevel() == 80) {
            jewelryEssence = 70;
        }else if (roleEquip.queryLevel()==90) {
            jewelryEssence =120;
        }else if (roleEquip.queryLevel()==100) {
            jewelryEssence =180;
        }
        jewelryEssence = jewelryEssence *2;
        if (roleEquip.queryLevel() > 100) {
            return "非法参数";
        }
        roleService.subtractJewelryEssence(role,jewelryEssence);
        roleService.subtractMoney(role,needCash);

        LinkedHashMap<Short, FiedValue> linkedHashMap = roleEquip.getFields().get((short) 514).getField();

        int size = linkedHashMap.size();
        roleEquip.getFields().get((short) 514).setField(new LinkedHashMap<>());
        for (int i = 0; i < size; i++) {
            equipService.randomField(equip, roleEquip, null, false);
        }
        roleEquip.addLimitTime(1);
        refreshRoleEquip(role,roleEquip);
        return MessageFormat.format(SUCCESS, AsktaoUtil.getMoneyFormat(needCash),jewelryEssence,roleEquip.getName());
    }


    @Override
    int getNotifyType() {
        return NotifyModules.NOTIFY_HIGHER_JEWELRY_RECAST_OK;
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if (roleEquip == null || !isRightPosition(roleEquip.queryEquipPos(false)) || roleEquip.queryLevel() < 80 || roleEquip.queryLevel() > 100) {
            return ERROR_RESOURCES;
        }
        Equip equip = EquipDataPool.getByName(roleEquip.getName());
        if (equip == null) {
            log.error("找不到装备配置=[{}]", param);
            return ERROR_RESOURCES;
        }
        String content = checkResource(role, roleEquip.queryLevel());
        if (content != null) {
            return content;
        }

        return doUpgrade(role, roleEquip,equip);
    }

    @Override
    boolean isRightPosition(byte equipPos) {
        return equipPos == NECKLACE || equipPos == BALDRIC || equipPos == LEFT_WRIST;
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        short level = (short) objects[1];
        if (role.getMoney() < needCash(level, 0)) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
        }

        int cost = Integer.MAX_VALUE;
        if (level == 80) {
            cost = 70;
        }else if (level==90) {
            cost =120;
        }else if (level==100) {
            cost =180;
        }

        if(role.getJewelryEssence()<cost){
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, "首饰精华");
        }
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        return level * 10000;
    }

    @Override
    public String getUpdateTypeStr() {
        return null;
    }
}
